﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using ForecastIO;

namespace BTv2.Controls
{
    public class WeatherController
    {
        private static WeatherController _instance;
        private ForecastIOReponse _wheaterForecast;
        private Timer _refreshTimer;
        private int[] gradient = new int[] { -84282550, -84283061, -84218804, -84219571, -84220591, -84287149, -84288428, -84223913, -84225446, -84226724, -84293792, -84230045, -84231321, -84232599, -101011093, -101012370, -101013902, -101080715, -117858951, -134637445, -134638211, -151612799, -168390267, -185168248, -185365363, -185366128, -185694062, -185956202, -203191909, -203716194, -204109406, -204764507, -205288792, -206009682, -206599504, -223900491, -224555848, -225210948, -242643519, -243363900, -243887927, -244542516, -261843761, -262301999, -263022126, -263349548, -263742506, -281044008, -281371176, -298148134, -298344486, -298344485, -315121444, -315121188, -315120932, -331897891, -331897634, -332028706, -348805409, -348805152, -365582367, -365647647, -365647647, -382424350, -382424094, -399200796, -399200796, -415977755, -415911963, -432754201, -432753689, -449399576, -449399319, -466241558, -466241558, -466241558, -466241558, -466241558, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

        public static WeatherController GetInstance()
        {
            if (_instance == null) _instance = new WeatherController();
            return _instance;
        }

        public WeatherController()
        {
            _refreshTimer = new Timer(1000 * 60 * 30);
            _refreshTimer.Elapsed += _refreshTimer_Elapsed;
            _refreshTimer.AutoReset = true;
            _refreshTimer.Enabled = true;
        }

        void _refreshTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Task.Run(() => {
                var request = new ForecastIORequest(Properties.Settings.Default.WheaterAPIKey, Properties.Settings.Default.WheaterXPos, Properties.Settings.Default.WheaterYPos, DateTime.Now, Unit.si);
                _wheaterForecast  = request.Get();
                NotificationsController.GetInstance().AddNotification("Weather Forcast Fetched", NotificationsController.NormalNotificationType);
            });
        }

        public bool PreloadWheater()
        {
            try
            {
                var request = new ForecastIORequest(Properties.Settings.Default.WheaterAPIKey, Properties.Settings.Default.WheaterXPos, Properties.Settings.Default.WheaterYPos, DateTime.Now, Unit.si);
                _wheaterForecast = request.Get();
                NotificationsController.GetInstance().AddNotification("Weather Forcast Fetched", NotificationsController.NormalNotificationType);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public ForecastIOReponse GetWeather()
        {
            if (_wheaterForecast == null)
            {
                var request = new ForecastIORequest(Properties.Settings.Default.WheaterAPIKey, Properties.Settings.Default.WheaterXPos, Properties.Settings.Default.WheaterYPos, DateTime.UtcNow, Unit.si);
                _wheaterForecast = request.Get();
            }
            return _wheaterForecast;
        }

        private async Task<int[]> FetchFrame(DateTime day, DateTime hour)
        {
            DateTime startday = day;
            DateTime starttime = hour;

            long dayval = 0; // (long)(startday - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
            long hourval = 0;// (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
            bool controlwhile = true;

            int[] orig = null;
            while (controlwhile)
            {

                var handler = new HttpClientHandler();
                var client = new HttpClient(handler);
             
                Uri uri = null;
                Stream respStream = null;
                try
                {
                    dayval = (long)(startday - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                    hourval = (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                    string path = Properties.Settings.Default.WheaterMap + dayval + ".png?" + hourval;
                    uri = new Uri(path);
                    respStream = await client.GetStreamAsync(uri);

                }
                catch
                {
                    try
                    {
                        dayval = (long)(startday.Subtract(TimeSpan.FromHours(6)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                        hourval = (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                        string path = Properties.Settings.Default.WheaterMap + dayval + ".png?" + hourval;
                        uri = new Uri(path);
                        respStream = client.GetStreamAsync(uri).Result;
                    }
                    catch
                    {
                        try
                        {
                            dayval = (long)(startday.AddHours(6) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                            hourval = (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                            string path = Properties.Settings.Default.WheaterMap + dayval + ".png?" + hourval;
                            uri = new Uri(path);
                            respStream = client.GetStreamAsync(uri).Result;
                        }
                        catch
                        {
                            try
                            {
                                dayval = (long)(startday.AddHours(12) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                                hourval = (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                                string path = Properties.Settings.Default.WheaterMap + dayval + ".png?" + hourval;
                                uri = new Uri(path);
                                respStream = client.GetStreamAsync(uri).Result;
                            }
                            catch
                            {
                                try
                                {
                                    dayval = (long)(startday.AddHours(18) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                                    hourval = (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                                    string path = Properties.Settings.Default.WheaterMap + dayval + ".png?" + hourval;
                                    uri = new Uri(path);
                                    respStream = client.GetStreamAsync(uri).Result;
                                }
                                catch
                                {
                                    //fuck it
                                }
                            }
                        }
                    }
                }
                controlwhile = false;
                try
                {
                    PngBitmapDecoder decoder = new PngBitmapDecoder(respStream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default);
                    BitmapSource bitmapSource = decoder.Frames[0];
                     orig =ConvertFrame( new WriteableBitmap(bitmapSource));
                    
                }
                catch
                {

                }

            }
            return orig;
        }
     
        private int[] ConvertFrame(WriteableBitmap wbm)
        {
            WriteableBitmap decodedmap = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Pbgra32, null);
            WriteableBitmap orig = wbm;

            int h = orig.PixelHeight;
            int w = orig.PixelWidth;
            int[] pixelDataOrig = new int[w * h];
            int[] pixelDataDecoded = new int[w * h];
            int widthInByte = 4 * w;

            decodedmap.CopyPixels(pixelDataDecoded, widthInByte, 0);
            orig.CopyPixels(pixelDataOrig, widthInByte, 0);

            double maxpixelvalue = pixelDataOrig.Select(x => BitConverter.GetBytes(x).Take(3).Average(z => z)).Max();

            for (int i = 0; i < pixelDataOrig.Length; i++)
            {

                if (pixelDataOrig[i] != -16777216)
                {

                    byte[] bytes = BitConverter.GetBytes(pixelDataOrig[i]);

                    int indexOfReplacementColor = (90 - (int)(90 * ((((new byte[] { bytes[0], bytes[1], bytes[2] }).Average(x => x))) / maxpixelvalue)));

                    pixelDataDecoded[i] = gradient[indexOfReplacementColor];

                }
                else
                {
                    pixelDataDecoded[i] = 0;
                }

            }

            return pixelDataDecoded;
        }


        public async Task<List<int[]>> FetchWheaterAnim()
        {

            List<KeyValuePair<int, Task<int[]>>> responses = new List<KeyValuePair<int, Task<int[]>>>();
            DateTime startday = (DateTime.Today.Subtract(TimeSpan.FromDays(6)));
            DateTime starttime = startday;

            long dayval = 0; // (long)(startday - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
            long hourval = 0;// (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;

            for (int i = 0; i < (24 * 13); i++)
            {
          
             if(i% 24 == 0)   startday = startday.AddDays(1);
                starttime = starttime.AddHours(i % 24);
                //dayval =(long)(startday.AddDays(((int)(i/24))) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                //hourval = (long)(starttime - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;;
                Task<int[]> newt = FetchFrame(startday, starttime);
                responses.Add(new KeyValuePair<int, Task<int[]>>(i,newt));

            }

            Task.WaitAll(responses.Select(x => x.Value).ToArray());

            List<int[]> finichedarr = responses.OrderBy(or => or.Key).Select(conv => conv.Value.Result).ToList();
            return  finichedarr;
        }


        public async Task<int[]> FetchWheaterMap()
        {
            var client = new HttpClient();
            //        http://darkskysatellite.s3.amazonaws.com/215159013250/1369483200.png?1369483200


            if (_wheaterForecast == null)
            {
                var request = new ForecastIORequest(Properties.Settings.Default.WheaterAPIKey, Properties.Settings.Default.WheaterXPos, Properties.Settings.Default.WheaterYPos, DateTime.UtcNow, Unit.si);
                _wheaterForecast = request.Get();
            }
            //var uri = new Uri("http://darkskysatellite.s3.amazonaws.com/215159013250/1369483200.png?1369483200");



            Uri uri = null;
            Stream respStream = null;
        

            try
            {
                long ltoday = (long)((DateTime.Today) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;

                long today =(long) Math.Round(((DateTime.Now.ToUniversalTime().Subtract( new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds) / 1e3) + 26);
                long ttt = today - today % 64800;
                string path = Properties.Settings.Default.WheaterMap + ttt + ".png?" + lnow;
                uri = new Uri(path);
                respStream = await client.GetStreamAsync(uri);
            }
            catch
            {
                try
                {
                    long ltoday = (long)((DateTime.Today.AddHours(6)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                    long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                    string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
                    uri = new Uri(path);
                    respStream = client.GetStreamAsync(uri).Result;
                }
                catch
                {
                    try
                    {
                        long ltoday = (long)((DateTime.Today.AddHours(12)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                        long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                        string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
                        uri = new Uri(path);
                        respStream = client.GetStreamAsync(uri).Result;
                    }
                    catch
                    {
                        try
                        {
                            long ltoday = (long)((DateTime.Today.AddHours(18)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                            long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                            string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
                            uri = new Uri(path);
                            respStream = client.GetStreamAsync(uri).Result;
                        }
                        catch
                        {
                            long ltoday = (long)((DateTime.Today.AddHours(24)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                            long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
                            string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
                            uri = new Uri(path);
                            respStream = client.GetStreamAsync(uri).Result;
                        }
                    }
                }
            }

            PngBitmapDecoder decoder = new PngBitmapDecoder(respStream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];
            WriteableBitmap decodedmap = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Pbgra32, null);
            WriteableBitmap orig = new WriteableBitmap(bitmapSource);

            int h = orig.PixelHeight;
            int w = orig.PixelWidth;
            int[] pixelDataOrig = new int[w * h];
            int[] pixelDataDecoded = new int[w * h];
            int widthInByte = 4 * w;

            decodedmap.CopyPixels(pixelDataDecoded, widthInByte, 0);
            orig.CopyPixels(pixelDataOrig, widthInByte, 0);

             int hjs = 0;
            // long[] p = new long[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 75448064, 162157824, 245586688, 347232512, 447764480, 565271552, 682776832, 817125888, 951147264, 1085496064, 1219779584, 1354062848, 5641966307, 1639275520, 1773362176, 1924488448, 2058705664, -2102109184, -1967825920, -1850385152, -1732944640, -1632346880, -1531618048, -1447731968, -1363845888, -1296737024, -1280025598, -1280091129, -1280156660, -1280287983, -1280288490, -1280354021, -1280485344, -1280485849, -1280551379, -1280682956, -1280683461, -1280814526, -1280945846, -1280946350, -1281143207, -1281143710, -1281340566, -1281340815, -1281472390, -1281603455, -1281668984, -1281800047, -1281865575, -1282062176, -1282127703, -1282258768, -1282455369, -1282455362, -1282586426, -1282717491, -1282914094, -1282979623, -1283110689, -1283372827, -1283438359, -1283569425, -1283766029, -1283831561, -1284028166, -1284224001, -1284289281, -1284419585, -1284680961, -1284745985, -1284942081, -1285137921, -1285333761, -1285529345, -1285790721, -1285986305, -1286247169, -1286443009, -1286704129, -1286899713, -1287160833, -1287487233, -1287748097, -1287943937, -1288139009, -1288400129, -1288660993, -1288856577, -1289117441, -1289312769, -1289573889, -1289769473, -1290030081, -1290225665, -1290420993, -1290616577, -1290812161, -1291007745, -1291203073, -1291267329, -1291463169, -1291527681, -1291657473, -1291787777, -1291786753, -1291785729, -1291784961, -1291784193, -1291783425, -1291782657, -1291782145, -1291781377, -1291780865, -1291780097, -1291780097, -1291649025, -1291649025, -1291452417, -1291321345, -1291190273, -1290993665, -1290797057, -1290600449, -1290403841, -1290207233, -1289945089, -1289748481, -1289486337, -1289224193, -1289027585, -1288699905, -1288437761, -1288110081, -1287913473, -1287585793, -1287258113, -1286930433, -1286602753, -1286275073, -1285947393, -1285619713, -1285292033, -1285029889, -1284636673, -1284374529, -1283981313, -1283588097, -1283325953, -1282867201, -1282539521, -1282211841, -1281884161, -1281556481, -1281294337, -1280901121, -1280507905, -1280245761, -1279852545, -1279590401, -1279328257, -1278935041, -1278672897, -1278345217, -1278083073, -1277820929, -1277558785, -1277231105, -1277034497, -1276772353, -1276510209, -1276313601, -1276116993, -1275920385, -1275723777, -1275592705, -1275396097, -1275199489, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275397377, -1275660545, -1275923713, -1276252673, -1276581633, -1276910593, -1277371137, -1277700097, -1278226433, -1278555393, -1279015937, -1279476481, -1280002817, -1280463361, -1280923905, -1281450241, -1282042369, -1282502913, -1283095041, -1283555585, -1284081921, -1284542977, -1285068801, -1285595137, -1286187265, -1286647809, -1287108353, -1287568897, -1288029441, -1288489985, -1288950529, -1289345281, -1289740033, -1290200577, -1290595329, -1290858497, -1291121665, -1291516417 };
           //  int[] p = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 162157824, 245586688, 245586688, 347232512, 447764480, 565271552, 682776832, 817125888, 1085496064, 1219779584, 1354062848, 1639275520, 1773362176, 1773362176, 1924488448, 1924488448, 2058705664, -2102109184, -1967825920, -1850385152, -1732944640, -1632346880, -1531618048, -1447731968, -1363845888, -1296737024, -1280025598, -1280091129, -1280156660, -1280287983, -1280288490, -1280354021, -1280485344, -1280485849, -1280551379, -1280682956, -1280683461, -1280814526, -1280945846, -1280946350, -1281143207, -1281143710, -1281340566, -1281340815, -1281472390, -1281603455, -1281668984, -1281800047, -1281865575, -1282062176, -1282127703, -1282258768, -1282455369, -1282455362, -1282586426, -1282717491, -1282914094, -1282979623, -1283110689, -1283372827, -1283438359, -1283569425, -1283766029, -1283831561, -1284028166, -1284224001, -1284289281, -1284419585, -1284680961, -1284745985, -1284942081, -1285137921, -1285333761, -1285529345, -1285790721, -1285986305, -1286247169, -1286443009, -1286704129, -1286899713, -1287160833, -1287487233, -1287748097, -1287943937, -1288139009, -1288400129, -1288660993, -1288856577, -1289117441, -1289312769, -1289573889, -1289769473, -1290030081, -1290225665, -1290420993, -1290616577, -1290812161, -1291007745, -1291203073, -1291267329, -1291463169, -1291527681, -1291657473, -1291787777, -1291786753, -1291785729, -1291784961, -1291784193, -1291783425, -1291782657, -1291782145, -1291781377, -1291780865, -1291780097, -1291780097, -1291649025, -1291649025, -1291452417, -1291321345, -1291190273, -1290993665, -1290797057, -1290600449, -1290403841, -1290207233, -1289945089, -1289748481, -1289486337, -1289224193, -1289027585, -1288699905, -1288437761, -1288110081, -1287913473, -1287585793, -1287258113, -1286930433, -1286602753, -1286275073, -1285947393, -1285619713, -1285292033, -1285029889, -1284636673, -1284374529, -1283981313, -1283588097, -1283325953, -1282867201, -1282539521, -1282211841, -1281884161, -1281556481, -1281294337, -1280901121, -1280507905, -1280245761, -1279852545, -1279590401, -1279328257, -1278935041, -1278672897, -1278345217, -1278083073, -1277820929, -1277558785, -1277231105, -1277034497, -1276772353, -1276510209, -1276313601, -1276116993, -1275920385, -1275723777, -1275592705, -1275396097, -1275199489, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275397377, -1275660545, -1275923713, -1276252673, -1276581633, -1276910593, -1277371137, -1277700097, -1278226433, -1278555393, -1279015937, -1279476481, -1280002817, -1280463361, -1280923905, -1281450241, -1282042369, -1282502913, -1283095041, -1283555585, -1284081921, -1284542977, -1285068801, -1285595137, -1286187265, -1286647809, -1287108353, -1287568897, -1288029441, -1288489985, -1288950529, -1289345281, -1289740033, -1290200577, -1290595329, -1290858497, -1291121665, -1291516417 };
             int[] p = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1639275520, 1639275520, 1773362176, 1773362176, 1924488448, 1924488448, 2058705664, -2102109184, -1967825920, -1850385152, -1732944640, -1632346880, -1531618048, -1447731968, -1363845888, -1296737024, -1280025598, -1280091129, -1280156660, -1280287983, -1280288490, -1280354021, -1280485344, -1280485849, -1280551379, -1280682956, -1280683461, -1280814526, -1280945846, -1280946350, -1281143207, -1281143710, -1281340566, -1281340815, -1281472390, -1281603455, -1281668984, -1281800047, -1281865575, -1282062176, -1282127703, -1282258768, -1282455369, -1282455362, -1282586426, -1282717491, -1282914094, -1282979623, -1283110689, -1283372827, -1283438359, -1283569425, -1283766029, -1283831561, -1284028166, -1284224001, -1284289281, -1284419585, -1284680961, -1284745985, -1284942081, -1285137921, -1285333761, -1285529345, -1285790721, -1285986305, -1286247169, -1286443009, -1286704129, -1286899713, -1287160833, -1287487233, -1287748097, -1287943937, -1288139009, -1288400129, -1288660993, -1288856577, -1289117441, -1289312769, -1289573889, -1289769473, -1290030081, -1290225665, -1290420993, -1290616577, -1290812161, -1291007745, -1291203073, -1291267329, -1291463169, -1291527681, -1291657473, -1291787777, -1291786753, -1291785729, -1291784961, -1291784193, -1291783425, -1291782657, -1291782145, -1291781377, -1291780865, -1291780097, -1291780097, -1291649025, -1291649025, -1291452417, -1291321345, -1291190273, -1290993665, -1290797057, -1290600449, -1290403841, -1290207233, -1289945089, -1289748481, -1289486337, -1289224193, -1289027585, -1288699905, -1288437761, -1288110081, -1287913473, -1287585793, -1287258113, -1286930433, -1286602753, -1286275073, -1285947393, -1285619713, -1285292033, -1285029889, -1284636673, -1284374529, -1283981313, -1283588097, -1283325953, -1282867201, -1282539521, -1282211841, -1281884161, -1281556481, -1281294337, -1280901121, -1280507905, -1280245761, -1279852545, -1279590401, -1279328257, -1278935041, -1278672897, -1278345217, -1278083073, -1277820929, -1277558785, -1277231105, -1277034497, -1276772353, -1276510209, -1276313601, -1276116993, -1275920385, -1275723777, -1275592705, -1275396097, -1275199489, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275068417, -1275397377, -1275660545, -1275923713, -1276252673, -1276581633, -1276910593, -1277371137, -1277700097, -1278226433, -1278555393, -1279015937, -1279476481, -1280002817, -1280463361, -1280923905, -1281450241, -1282042369, -1282502913, -1283095041, -1283555585, -1284081921, -1284542977, -1285068801, -1285595137, -1286187265, -1286647809, -1287108353, -1287568897, -1288029441, -1288489985, -1288950529, -1289345281, -1289740033, -1290200577, -1290595329, -1290858497, -1291121665, -1291516417 };
            
            for (int i = 0; i < pixelDataDecoded.Length; i++)
             {
                 hjs =(int)( 0.5 * (pixelDataOrig[i] >> 0 & 255) + 0.25 * (pixelDataOrig[i] >> 8 & 255) + 0.25 * (pixelDataOrig[i] >> 0 & 255) + 0 * (pixelDataOrig[i] >> 16 & 255));

                 
                  byte[] bytes = BitConverter.GetBytes((int)p[(hjs) | 0]);
                 
                 pixelDataDecoded[i] = (BitConverter.ToInt32((new byte[] { bytes[2], bytes[1], bytes[0], bytes[3]}),0));
               //  pixelDataDecoded[i] =(int)p[hjs|0];
             }
             //for (int i = 0; i < 256; i++)
             //{
             //    byte[] bytes = BitConverter.GetBytes((int)p[i | 0]);
                 
             //    pixelDataDecoded[i] = (BitConverter.ToInt32((new byte[] { bytes[2], bytes[1], bytes[0], bytes[3]}),0));
             //}
            
            //double maxpixelvalue = pixelDataOrig.Select(x => BitConverter.GetBytes(x).Take(3).Average(z => z)).Max();

            //for (int i = 0; i < pixelDataOrig.Length; i++)
            //{

            //    if (pixelDataOrig[i] != -16777216)
            //    {

            //        byte[] bytes = BitConverter.GetBytes(pixelDataOrig[i]);

            //        int indexOfReplacementColor = (90 - (int)(90 * ((((new byte[] { bytes[0], bytes[1], bytes[2] }).Average(x => x))) / maxpixelvalue)));

            //        pixelDataDecoded[i] = gradient[indexOfReplacementColor];

            //    }
            //    else
            //    {
            //        pixelDataDecoded[i] = 0;
            //    }

            //}

            return pixelDataDecoded;
        }

        //public async Task<int[]> FetchWheaterMap()
        //{
        //    var client = new HttpClient();
        //    //        http://darkskysatellite.s3.amazonaws.com/215159013250/1369483200.png?1369483200
                                                                                       

        //    if (_wheaterForecast == null)
        //    {
        //        var request = new ForecastIORequest(Properties.Settings.Default.WheaterAPIKey, Properties.Settings.Default.WheaterXPos, Properties.Settings.Default.WheaterYPos, DateTime.UtcNow, Unit.si);
        //        _wheaterForecast = request.Get();
        //    }
        //    //var uri = new Uri("http://darkskysatellite.s3.amazonaws.com/215159013250/1369483200.png?1369483200");

           
            
        //    Uri uri = null;
        //    Stream respStream = null;
        //    try
        //    {
        //        long ltoday = (long)((DateTime.Today) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //        long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //        string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
        //        uri = new Uri(path);
        //        respStream = await client.GetStreamAsync(uri);
        //    }
        //    catch
        //    {
        //        try
        //        {
        //            long ltoday = (long)((DateTime.Today.AddHours(6)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //            long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //            string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
        //            uri = new Uri(path);
        //            respStream = client.GetStreamAsync(uri).Result;
        //        }
        //        catch
        //        {
        //            try
        //            {
        //                long ltoday = (long)((DateTime.Today.AddHours(12)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //                long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //                string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
        //                uri = new Uri(path);
        //                respStream = client.GetStreamAsync(uri).Result;
        //            }
        //            catch
        //            {
        //                try
        //                {
        //                    long ltoday = (long)((DateTime.Today.AddHours(18)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //                    long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //                    string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
        //                    uri = new Uri(path);
        //                    respStream = client.GetStreamAsync(uri).Result;
        //                }
        //                catch
        //                {
        //                    long ltoday = (long)((DateTime.Today.AddHours(24)) - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //                    long lnow = (long)(DateTime.Now - (new DateTime(1970, 1, 1, 0, 0, 0))).TotalSeconds;
        //                    string path = Properties.Settings.Default.WheaterMap + ltoday + ".png?" + lnow;
        //                    uri = new Uri(path);
        //                    respStream = client.GetStreamAsync(uri).Result;
        //                }
        //            }
        //        }
        //    }

        //    PngBitmapDecoder decoder = new PngBitmapDecoder(respStream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.Default);
        //    BitmapSource bitmapSource = decoder.Frames[0];
        //    WriteableBitmap decodedmap = new WriteableBitmap(500, 500, 96, 96, PixelFormats.Pbgra32, null);
        //    WriteableBitmap orig = new WriteableBitmap(bitmapSource);

        //    int h = orig.PixelHeight;
        //    int w = orig.PixelWidth;
        //    int[] pixelDataOrig = new int[w * h];
        //    int[] pixelDataDecoded = new int[w * h];
        //    int widthInByte = 4 * w;

        //    decodedmap.CopyPixels(pixelDataDecoded, widthInByte, 0);
        //    orig.CopyPixels(pixelDataOrig, widthInByte, 0);

        //    double maxpixelvalue = pixelDataOrig.Select(x => BitConverter.GetBytes(x).Take(3).Average(z => z)).Max();

        //    for (int i = 0; i < pixelDataOrig.Length; i++)
        //    {

        //        if (pixelDataOrig[i] != -16777216)
        //        {

        //            byte[] bytes = BitConverter.GetBytes(pixelDataOrig[i]);

        //            int indexOfReplacementColor = (90 - (int)(90 * ((((new byte[] { bytes[0], bytes[1], bytes[2] }).Average(x => x))) / maxpixelvalue)));

        //            pixelDataDecoded[i] = gradient[indexOfReplacementColor];

        //        }
        //        else
        //        {
        //            pixelDataDecoded[i] = 0;
        //        }

        //    }

        //    return pixelDataDecoded;
        //}
   
    }
}
